package com.lx.oms.controller.controller;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lx.oms.common.annotation.SysLog;
import com.lx.oms.common.constant.MsgConstant;
import com.lx.oms.common.controller.AbstractController;
import com.lx.oms.common.entity.Page;
import com.lx.oms.common.entity.R;
import com.lx.oms.controller.entity.AccountEntity;
import com.lx.oms.controller.entity.AccountLogEntity;
import com.lx.oms.controller.service.AccountLogService;
import com.lx.oms.controller.service.AccountService;

/**
 * 账户余额
 *
 * @author houzhong
 * @date 2018年10月30日 下午2:02:43
 */
@RestController
@RequestMapping("/sys/account")
public class AccountController extends AbstractController {
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private AccountLogService accountLogService;
	
	/**
	 * 列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	public Page<AccountEntity> list(@RequestBody Map<String, Object> params) {
		return accountService.listAccount(params);
	}
		
	/**
	 * 新增
	 * @param tAccount
	 * @return
	 */
	@SysLog("新增账户余额")
	@RequestMapping("/save")
	public R save(@RequestBody AccountEntity tAccount) {
		R r = null;
		AccountEntity accountEntity = accountService.getAccountId(new BigInteger(tAccount.getUserId()));
		if (accountEntity != null) {
			accountEntity.setAmount(accountEntity.getAmount() + tAccount.getPrice());
			accountService.updateAccount(accountEntity);
			
			AccountLogEntity logEntity = new AccountLogEntity();
			//设置订单编号
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String time = df.format(new Date());
			logEntity.setAccountId(accountEntity.getId());
			
			logEntity.setBusinessNumber(time + random6());
			logEntity.setBusinessType("1");
			logEntity.setPrice(tAccount.getPrice());
			logEntity.setUserId(tAccount.getUserId());
			logEntity.setAmount(accountEntity.getAmount());
			logEntity.setType(2);
			
			r = accountLogService.saveAccountLog(logEntity);
		}else {
			r = R.error(MsgConstant.MSG_OPERATION_FAILED);
		}
		
		return r;
	}
	
	/**
	 * 根据id查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/info")
	public R getById(@RequestBody String id) {
		return accountService.getAccountById(new BigInteger(id));
	}
	
	/**
	 * 修改
	 * @param tAccount
	 * @return
	 */
	@SysLog("修改账户余额")
	@RequestMapping("/update")
	public R update(@RequestBody AccountEntity tAccount) {
		return accountService.updateAccount(tAccount);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@SysLog("删除账户余额")
	@RequestMapping("/remove")
	public R batchRemove(@RequestBody String[] id) {
		return accountService.batchRemove(id);
	}
	
	public static int random6() {
		Random random = new Random();
		int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 1000000;
		return rannum;
	}
	
}
